package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.AdUserService;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.BCrypt;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liuyaning
 * @version 1.0
 * @date 2021/7/30 15:38
 */
@Service
@Transactional
@Slf4j
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService {
    @Override
    public ResponseResult in(AdUserDto adUserDto) {
        //判断参数
        if (adUserDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断用户名和密码是否都填写
        if (StringUtils.isBlank(adUserDto.getName()) || StringUtils.isBlank(adUserDto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"用户名和密码不能为空");
        }
        //根据用户名查询是否存在该用户
        AdUser adUser = getOne(Wrappers.<AdUser>lambdaQuery().eq(AdUser::getName, adUserDto.getName()));
        if (adUser == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"未找到该用户");
        }
        //如果存在，比较密码
        String loginPwd = adUserDto.getPassword();//用户登陆填写的铭文密码
        String dbPwd = adUser.getPassword();//数据库中存储的加密密码
        //checkpw（明文密码，加密后的密码）
        boolean flag = BCrypt.checkpw(loginPwd, dbPwd);
        if(flag){
            //如果登陆成功则用AppJwtUtil根据用户id生成token
            String token = AppJwtUtil.getToken(adUser.getId().longValue());

            //前台需要返回的信息data中包含user和token 所以封装进map中
            //将用户密码设置为空
            adUser.setPassword("");
            Map<String,Object> map = new HashMap<>();
            map.put("user",adUser);
            map.put("token",token);
            return ResponseResult.okResult(map);
        }else{
            //密码错误
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
    }

    @Override
    public ResponseResult register(AdUser adUser) {
        //判断参数
        if (adUser == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断用户名是否存在
        AdUser registerUser = getOne(Wrappers.<AdUser>lambdaQuery().eq(AdUser::getName, adUser.getName()));
        if(registerUser != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该用户已存在");
        }
        //不存在则保存数据到db
        //密码加密BCrypt
        String registerPwd = adUser.getPassword();//用户填写的密码
        String gensalt = BCrypt.gensalt();//自动加盐
        String newPassword = BCrypt.hashpw(registerPwd, gensalt);
        adUser.setPassword(newPassword);
        adUser.setCreatedTime(new Date());
        adUser.setLoginTime(new Date());
        //保存用户信息
        save(adUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
